package dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import hibernate.entity.Teacher;

@Repository
@Transactional
public class TeacherHibernateDao {

	@Autowired
	protected SessionFactory sessionFactory;
	
	public List<Teacher> findAll(){
		
		return (List<Teacher>)sessionFactory.getCurrentSession().createCriteria(Teacher.class).list();
	}
	
	public long count(){
		
		return findAll().size();
	}
	
	public void save(Teacher person){
		
		sessionFactory.getCurrentSession().save(person);
	}
	
	public Teacher get(int id){
		
		return (Teacher)sessionFactory.getCurrentSession().get(Teacher.class, id);
	}
	
	public Teacher getByName(String name){
		
		return (Teacher)sessionFactory.getCurrentSession()
				.createCriteria(Teacher.class)
				.add(Restrictions.eq("t_name", name));
	}
}
